package com.bnz.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bnz.mapper.StudentMapper;
import com.bnz.pojo.Classes;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.Student;
import com.bnz.service.ClassesService;
import com.bnz.service.StudentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper,Student>implements StudentService {

    @Autowired
   private ClassesService classesService;
    @Override
    public List<Student> findAll() {
        return baseMapper.selectList(null);
    }

    @Override
    public PageResult<Student> findPyPage(Integer page, Integer pageSize) {
        //查出page对象
        Page<Student> page1 = this.page(new Page<>(page, pageSize), null);
//        绑定班级
        for (Student record : page1.getRecords()) {
            Classes byId = classesService.getById(record.getCid());
            record.setCname(byId.getCname());
        }
        //        转为result对昂
        return new PageResult<>(page1.getTotal(),page1.getRecords());
    }

    @Override
    public PageResult<Student> search(Integer page, Integer pageSize, Student student) {
        //定义条件
        //设hi查询条件
//                .like("sname",student.getSname())
//                .like("addr",student.getAddr());
//        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        //(((((-----------------链式语法查询------------)))))
        QueryChainWrapper<Student> wrapper = this.query();
        this.query()
                .like(StringUtils.isNotBlank(student.getSname()),"sname",student.getSname())
                .like(StringUtils.isNotBlank(student.getAddr()),"addr",student.getAddr())
                .eq(student.getCid()!=null&& student.getCid()!=0,"cid",student.getCid()  );

        //(((((=-------lambda表达式---------))))
//        this.lambdaQuery()
//                .like(StringUtils.isNotBlank(student.getSname()),Student::getSname,student.getSname())
//                .like(StringUtils.isNotBlank(student.getAddr()),student::getAddr,student.getAddr())
//                .eq(student.getCid()!=0 &&student.getCid()!=null,student::getCid,student.getCid())
//                .page(new Page<>(page,pageSize));
//
//        @开始分页
        Page<Student> page1 = wrapper.page(new Page<>(page, pageSize));
        //返回Result
        return new PageResult<>(page1.getTotal(),page1.getRecords());
    }

    //怎加
    @Override
    public void add(Student student) {
        this.save(student);
    }
    @Override
    public void update(Student student) {
        this.update(student);
    }

    @Override
    public void delete(String[] ids) {
        this.removeById(ids);
    }
}
